<template>
  <div id="login">
    <div class="login_content">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/img/logo.jpg" alt="坚果小公主" />
      </div>
      <!-- 表单区域 -->
      <el-form
        ref="loginFromRef"
        :model="loginForm"
        :rules="loginFromRules"
        label-width="0px"
        class="form-box"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  data() {
    return {
      // 这是登录绑定的数据用户名和密码
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      // 这是检验用户名和密码的规则
      loginFromRules: {
        username: [
          { required: true, message: '请输入登录账号', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          {
            min: 6,
            max: 10,
            message: '长度在 6 到 10 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    // 重置
    reset() {
      this.$refs.loginFromRef.resetFields()
      // console.log(this);
    },
    login() {
      this.$refs.loginFromRef.validate(async (valid) => {
        if (!valid){
           return this.$message.error("表单提交校验失败");
        }
        const { data: res } = await this.$axios.post('login', this.loginForm)
        // console.log(res);
        if (res.meta.status !== 200) return this.$message.error('登录失败!')
        this.$message.success('登录成功！')
        // 1、将登录之后的token，保存到客户端的sessionStorage中
        //  1.1 项目中除了登录之外的其他API接口，必须在登录之后才能访问
        //  1.2 token只应在当前网站打开期间生效，所以将token保存在sessionStorge中
        // 2、通过编程式导航跳转到后台主页，路由地址是/home
        // console.log(res);
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
  },
}
</script>

<style  scoped>
#login {
  height: 100%;
  background-color: #2b4b6b;
}
#login .login_content {
  position: absolute;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
#login .login_content .avatar_box {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 120px;
  height: 120px;
  border: solid 1px #eee;
  border-radius: 50%;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0 0 10px #ddd;
}
#login .login_content .avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}
#login .login_content .form-box {
  position: absolute;
  bottom: 10px;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
}
#login .login_content .form-box .btns {
  display: flex;
  justify-content: flex-end;
}
</style>